package com.cxm.support.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.cxm.support.entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

@Controller

public class HtmlController {

    @GetMapping("/denglu")
    public String denglu() {

        return "denglu";
    }
    @GetMapping("/support/userlist")
    public ModelAndView redirect8() {
        ModelAndView modelAndView =new ModelAndView();
        modelAndView.setViewName("userlist");
        return modelAndView;
    }


    @GetMapping("/support/mychart")
    public ModelAndView redirect2() {
        ModelAndView modelAndView =new ModelAndView();
        modelAndView.setViewName("chartlist");
        return modelAndView;
    }

    @GetMapping("/support/knowledgelist")
    public ModelAndView redirect7() {
        ModelAndView modelAndView =new ModelAndView();
        modelAndView.setViewName("knowledgelist");
        return modelAndView;
    }



    @GetMapping("/support/workchartlist")
    public ModelAndView redirect6() {
        ModelAndView modelAndView =new ModelAndView();
        modelAndView.setViewName("workchartlist");
        return modelAndView;
    }

    @GetMapping("/support/waitchartlist")
    public ModelAndView redirect5() {
        ModelAndView modelAndView =new ModelAndView();
        modelAndView.setViewName("waitchartlist");
        return modelAndView;
    }

    @GetMapping("/support/index")
    public ModelAndView redirect0() {
        ModelAndView modelAndView =new ModelAndView();
        modelAndView.setViewName("index");
//        modelAndView.addObject("videokindlist",videokindService.list());
        return modelAndView;
    }
    @GetMapping("/support/create")
    public ModelAndView redirect1() {
        ModelAndView modelAndView =new ModelAndView();
        modelAndView.setViewName("create");
//        modelAndView.addObject("videokindlist",videokindService.list());
        return modelAndView;
    }

    //登录模块


    @PostMapping("/login")
    @ResponseBody
    public Serializable login(String username, String password){

        Subject subject= SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(username,password);
        try {
//验证密码是否正确
            subject.login(token);
            User user= (User) subject.getPrincipal();
            subject.getSession().setAttribute("user",user);
//            session.setAttribute("userinfo",userinfo);

            return 0;
        } catch (UnknownAccountException e) {
            e.printStackTrace();

            return "没有此用户";
        } catch(IncorrectCredentialsException e){
            e.printStackTrace();

            return  "密码错误";
        }



    }



























//    @GetMapping("/videomanagement/videolist")
//    public String redirect2() {
//        return "videolist";
//    }
//    @GetMapping("/videomanagement/video")
//    public String redirect3(HttpSession session) {
//        session.setAttribute("channel",null);
//        session.setAttribute("videoname",null);
//        return "videolist";
//    }
//    @GetMapping("/videomanagement/videolist/{channel}")
//    public String redirect4(@PathVariable String channel, HttpSession session) {
//     session.setAttribute("channel",channel);
//     session.setAttribute("videoname",null);
//
//
//
//
//        return "videolist";
//    }
//    @GetMapping("/test")
//    public String redirect7() {
//        return "test";
//    }
//    @GetMapping("/test2")
//    @ResponseBody
//    public List redirect72() {
//        QueryWrapper wrapper = new QueryWrapper();
//        String strs=",娱乐,online,体育";
//        wrapper.in("author",Arrays.asList(strs.split(",")));
//        System.out.println(videoService.list(wrapper));
//        return     videoService.list(wrapper);
////        ArrayList<ArrayList> arrayLists = new ArrayList<>();
////        arrayLists=[[1,2],[3,4]];
//    }



//登录模块

//
//    @PostMapping("/login")
//    @ResponseBody
//    public Serializable login(String username, String password){
//
//        Subject subject= SecurityUtils.getSubject();
//        UsernamePasswordToken token=new UsernamePasswordToken(username,password);
//        try {
////验证密码是否正确
//            subject.login(token);
//            User user= (User) subject.getPrincipal();
//            subject.getSession().setAttribute("user",user);
////            session.setAttribute("userinfo",userinfo);
//
//            return 0;
//        } catch (UnknownAccountException e) {
//            e.printStackTrace();
//
//            return "没有此用户";
//        } catch(IncorrectCredentialsException e){
//            e.printStackTrace();
//
//            return  "密码错误";
//        }
//
//
//
//    }


































}
